<template>
  <div class="PID">
    <div style="margin:15px 0 15px 15px">
      <el-radio-group v-model="selectType" size="medium" @change="getChangeLie">
        <el-radio-button key="cpu" label="cpu"
          >按cpu使用率TOP10</el-radio-button
        >
        <el-radio-button key="mem" label="mem"
          >按内存使用率TOP10</el-radio-button
        >
      </el-radio-group>
    </div>
    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      :data="tableDataFn"
      :header-cell-style="{
          background:'#E0E0E0'
        }"
      stripe
      style="width: 100%"
      class="tableData-box"
    >
      <el-table-column type="index" width="50" label="序号"> </el-table-column>
      <template v-for="item in tableColumns">
        <el-table-column
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
          align="center"
          v-if="item.isShow"
          :key="item.prop"
        >
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>

<script>
import { getHostProcess } from "@/api/index.js";
import tableColumns from "./PIDTopTableColumns.js";
import bus from "common/bus.js";
import { GetItem } from "@/utils/dataStore";
export default {
  components: {
  },
  data() {
    return {
      postData: {
        opera: "hostconnect",
        curPage: 1,
        pageSize: 20,
        sort: {
          sortColumn: "",
          sortOrders: "",
        },
        filtersType: "exactquery",
        filters: {},
      },

      tableData: [],
      dataTotal: 0,
      tableColumns: tableColumns,
      SearchIPArr: [],
      searchJson: {},
      SearchIP: "",
      timerName: "MonitorInfoServerConnect",
      timerEventName: "timerMonitorInfoServerConnect",
      timerInterval: 1000 * 60*5,
      loading: true,
      selectedMonID: "",
      selectType: "cpu",
    };
  },
  created() {
    GetItem(sessionStorage.getItem('PushId')).then(_ => {
      this.selectedMonID =_[0].articleAddress;
      this.postData.filters.ip =this.selectedMonID.IP ;
      this.getTableData();
    })

    bus.$emit("timerNewTodo", {
      name: this.timerName,
      eventName: this.timerEventName,
      interval: this.timerInterval,
      intervalNow: 0,
    });

    bus.$on(this.timerEventName, (msg) => {
      if (msg.isGetData) {
        this.getTableData();
      }
    });
  },
  mounted() {
    bus.$on("ItemMonitorServer", (msg) => {
      this.resetData();
      this.selectedMonID = msg;
      this.postData.filters.ip = this.selectedMonID.IP;

      this.getTableData();
    });
  },
  computed: {
    tableDataFn() {
      return this.tableData.filter((item) => {
        return item;
      });
    },
  },
  methods: {
    getTableData() {
      this.loading = true;
      if (!this.postData.filters.ip) {
        return;
      }
      getHostProcess({ type: this.selectType, ip: this.postData.filters.ip })
        .then((res) => {
          let header = res[0].split(" ").filter((item) => item);
          let body = [];

          for (let index = 1; index < res.length; index++) {
            let item = res[index].split(" ").filter((item) => item);
            let itemInfo = {};
            header.forEach((element, indexs) => {
              itemInfo[element] = item[indexs];
              if (indexs == header.length - 1) {
                body.push(itemInfo);
              }
            });
          }

          this.dataTotal = body.length;
          this.tableData = body;

          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.tableData = [];
        });
    },
    getPaginationCurPage(val) {
      this.postData.curPage = val;
      this.getTableData();
    },
    getPaginationPageSize(val) {
      // 分页大小改变之后，重置当前页码为 1
      this.postData.curPage = 1;
      this.postData.pageSize = val;

      this.getTableData();
    },
    getChangeLie(val) {
      this.selectType = val;
      this.getTableData();
    },
    resetData() {
      this.dataTotal = 0;
      this.tableData = [];
    },
  },
  watch: {},
  beforeDestroy() {
    // 页面关闭前，移除定时器任务
    bus.$emit("timerNewTodo", {
      name: this.timerName,
    });
  },
};
</script>

<style scoped>
.el-table .warning-row {
  background: oldlace;
}
.el-table .success-row {
  background: #f0f9eb;
}
.PID /deep/ .el-table thead {
  color: black;
}

.tableData-box{
    height: calc(100% - 30px);
}
.tableData-box > .el-col {
    height: 100%;
}
.tableData-box /deep/ .el-table__body-wrapper {
  overflow-y: scroll;
  height: 270px;
}
.tableData-box /deep/ .el-table__body-wrapper::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}
.tableData-box /deep/ .el-table__body-wrapper::-webkit-scrollbar-thumb {
    border-radius: 3px;
    background-color: #E0E0E0;
}
</style>
